import os

from src.parse_project.Project_Introduction2 import Project_Introduction_main2
# from classify import class_main, main
from src.parse_project.classifyClass import classify
from src.parse_project.Data_structure_analysis import Data_structure_analysis_main
from src.parse_project.Project_Introduction import Project_Introduction_main
# from src.parse_project.Project_Introduction import Project_Introduction_main
from src.parse_project.finishing_depend import finishing_depend_main
from src.parse_project.Activity_diagram import Activity_diagram_main
from src.parse_project.entity.depend_data import DependData
from typing import List


# 查找src文件
def find_src(path):
    for i in os.listdir(path):
        if i == "src":
            path_file = os.path.join(path, i)  # 取文件绝对路径
            return path_file
        # if i == ''
        if not i == ".idea" and not i == ".git":
            path_file = os.path.join(path, i)  # 取文件绝对路径
            if not os.path.isfile(path_file):
                src_path = find_src(path_file)
                return src_path

    # 解析之后删除文件


# 存储路径的list
controller_list = []
# controller_list = ["../../java/project/backend/src/main/java/com/open/harmony/controller/OrderController.java"]
service_list = []
mapper_list = []
dao_list = []
# dao_list = ["../../java/project/backend/src/main/java/com/open/harmony/entity/Order.java"]


# 目录


def read_file(path, indent):
    directory = ""  # 使用局部变量来构建目录字符串
    for i in os.listdir(path):
        if i not in [".idea", ".git", "test", "resources"]:
            path_file = os.path.join(path, i)
            # 如果是目录，则递归调用
            if os.path.isdir(path_file):
                directory += f"{indent}|-- {i}\n"
                directory += read_file(path_file, indent + "    ")
            # 如果是文件，则根据类型添加到相应的列表中
            elif os.path.isfile(path_file):
                directory += f"{indent}|-- {i}\n"
                type_data = classify(path_file)
                if type_data == "Controller":
                    controller_list.append(path_file)
                elif type_data == "Mapper":
                    mapper_list.append(path_file)
                elif type_data == "Service":
                    service_list.append(path_file)
                elif type_data == "Dao":
                    dao_list.append(path_file)
                # 可以选择在这里打印文件信息，但通常这不是构建目录树的目的
                # print(f"{indent}|   {i}")
    return directory


def analyse(directory):
# def analyse():
    with open('report.md', 'w', encoding='utf-8') as file:
        data: List[DependData] = finishing_depend_main(controller_list, service_list, mapper_list, dao_list)

        # 项目功能介绍
        file.write("\n\n## 项目功能介绍\n\n")
        if os.path.exists("../../java/project/README.md"):
            Project_Introduction = Project_Introduction_main("../../java/project/README.md")

            file.write(f"\n\n{Project_Introduction}\n\n")
        else:
            Project_Introduction = "这是一个java项目"
            for index, item in enumerate(controller_list):
                Project_Introduction = Project_Introduction_main2(item,Project_Introduction)
            for index, item in enumerate(dao_list):
                Project_Introduction = Project_Introduction_main2(item,Project_Introduction)
            for index, item in enumerate(service_list):
                Project_Introduction = Project_Introduction_main2(item,Project_Introduction)
            for index, item in enumerate(mapper_list):
                Project_Introduction = Project_Introduction_main2(item,Project_Introduction)
            file.write(f"\n\n{Project_Introduction}\n\n")

        # 代码结构介绍
        file.write("\n\n## 项目结构\n\n")
        file.write(f"\n\n{directory}\n\n")
        # 对数据结构进行分析
        file.write("\n\n## 数据结构分析\n\n")
        for index,item in enumerate(dao_list):
            file.write(f"\n\n### 文件 {item[5:]} 的数据结构分析:\n\n")
            # 调用函数并获取结果
            data = Data_structure_analysis_main(item)
            # 假设data是一个可以直接写入的字符串；如果不是，请先进行转换
            file.write(data.content)  # 可能需要在data后添加一个换行符以保持格式

        # 接口文档介绍
        file.write("\n\n## 接口文档分析\n\n")
        for index,item in enumerate(controller_list):
            file.write(f"\n\n### 文件 {item[5:]} 的数据结构分析:\n\n")
            # 调用函数并获取结果
            data = Data_structure_analysis_main(item)
            # 假设data是一个可以直接写入的字符串；如果不是，请先进行转换
            file.write(data.content)  # 可能需要在data后添加一个换行符以保持格式

        # 核心模块代码解读
        file.write("\n\n## 核心代码分析\n\n")
        for index, item in enumerate(service_list):
            file.write(f"\n\n### 文件 {item[5:]} 的数据结构分析:\n\n")
            # 调用函数并获取结果
            data = Data_structure_analysis_main(item)
            # 假设data是一个可以直接写入的字符串；如果不是，请先进行转换
            file.write(data.content)  # 可能需要在data后添加一个换行符以保持格式

        for index, item in enumerate(mapper_list):
            file.write(f"\n\n### 文件 {item[5:]} 的数据结构分析:\n\n")
            # 调用函数并获取结果
            data = Data_structure_analysis_main(item)
            # 假设data是一个可以直接写入的字符串；如果不是，请先进行转换
            file.write(data.content)  # 可能需要在data后添加一个换行符以保持格式


    # 文件现在会在with语句块结束时自动关闭

    file.close()


def main():
    path = r'./java'
    src_path = find_src(path)
    directory = read_file(src_path, " ")
    print(directory)
    # analyse()
    analyse(directory)


if __name__ == '__main__':
    main()